<template>
	<view class="addEditForm">
		<view class="data-form bgfff">
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30 require">出库单</text>
				<!-- <view class="data-form-input">
					<cpdd-select v-model="dataForm.outId" :disabled="optionDisabled" :list="selectList"
						keyname="name" valname="val"></cpdd-select>
				</view> -->
				<view class="data-form-input" @click="openSelect">
					<input type="text" readyonly="readyonly" v-model="dataForm.outId"
						:placeholder="optionDisabled?'-':'请选择采购单'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30 require">添加剂名称</text>
				<view class="data-form-input">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.name"
						:placeholder="optionDisabled?'-':'请输入添加剂名称'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30 require">添加的食物</text>
				<view class="data-form-input">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.foodName"
						:placeholder="optionDisabled?'-':'请输入添加的食物'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30 require">添加的数量</text>
				<view class="data-form-input">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.quantity"
						:placeholder="optionDisabled?'-':'请输入添加的数量'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30 require">单位</text>
				<view class="data-form-input">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.unit"
						:placeholder="optionDisabled?'-':'请输入单位'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30 require">使用时间</text>
				<view class="data-form-input">
					<cpdd-date v-model="dataForm.useTime" :disabled="optionDisabled" :isTime="false"></cpdd-date>
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30 require">操作员</text>
				<!-- <view class="data-form-input">
					<cpdd-select v-model="dataForm.useBy" :disabled="optionDisabled" :list="selectList"
						keyname="name" valname="val"></cpdd-select>
				</view> -->
				<view class="data-form-input" @click="openSelectUser">
					<input type="text" :disabled="optionDisabled" v-model="dataForm.useByName"
						:placeholder="optionDisabled?'-':'请选择操作员'" placeholder-class="plStyle" />
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30 require">生产日期</text>
				<view class="data-form-input">
					<cpdd-date v-model="dataForm.produceTime" :disabled="optionDisabled" :isTime="false"></cpdd-date>
				</view>
			</view>
			<view class="data-form-item flex-ac">
				<text class="data-form-label f30 require">过期日期</text>
				<view class="data-form-input">
					<cpdd-date v-model="dataForm.expireTime" :disabled="optionDisabled" :isTime="false"></cpdd-date>
				</view>
			</view>

		</view>
		<view class="right-area-footer2 flex-ac bgfff" v-if="!optionDisabled">
			<view class="saveBtn cancleBtn" @tap="handleCancle">取消</view>
			<view class="saveBtn" @tap="handleSubmit">保存</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				did: '',
				dataForm: { //表单
					id: '',
					outId: '',
					name: '',
					foodName: '',
					quantity: '',
					unit: '',
					useTime: '',
					useBy: '',
					produceTime: '',
					expireTime: ''
				},
				cpddDateRangeVal: '',
				cpddDateRangeVal2: '',
				selectList: [], //上一页带过来，如果上一页用不到，可以在这里调用赋值
				optionDisabled: false,
			}
		},
		onLoad(options) {
			console.log('options', options);
			this.did = options.id || ''
			this.optionDisabled = options.op == 'look' ? true : false
			let title = options.op == 'look' ? '详情' : options.op == 'add' ? '新增' : '编辑'
			uni.setNavigationBarTitle({
				title
			})
			if (options.op != 'add') {
				this.loadDetail();
			}
			uni.$on('selectCkd', (data) => {
				console.log(data);
				this.dataForm.outId = data.id;
				this.$forceUpdate()
			})
			uni.$on('selectCyry', (data) => {
				console.log(data);
				console.log("==================");
				this.dataForm.useBy = data.userId;
				this.dataForm.useByName = data.nickName;
				this.$forceUpdate()
			})
		},
		onUnload() {
			uni.$off('selectCkd')
			uni.$off('selectCyry')
		},
		methods: {
			loadDetail() {
				this.$http.get(`/material/foodAddRecord/${this.did}`).then(res => {
					if (res.code == 200) {
						Object.keys(this.dataForm).forEach(key => this.dataForm[key] = res.data[key])
						this.dataForm.useByName = res.data.params.useByName;
					}
				})
			},
			openSelect() {
				this.$u.to(`/pages/ylgl/ckd-warp/index?type=select`)
			},
			openSelectUser() {
				this.$u.to(`/pages/cyrygl/cyry-warp/index?type=select`)
			},
			handleCancle() {
				this.$u.back()
			},
			handleSubmit() {
				console.log('提交');
				if (!this.dataForm.outId) return this.$u.msg("请选择出库单");
				if (!this.dataForm.name) return this.$u.msg("请输入添加剂名称");
				if (!this.dataForm.foodName) return this.$u.msg("请输入添加的食物");
				if (!this.dataForm.quantity) return this.$u.msg("请输入添加的数量");
				if (!this.dataForm.unit) return this.$u.msg("请输入单位");
				if (!this.dataForm.useTime) return this.$u.msg("请选择使用时间");
				if (!this.dataForm.useBy) return this.$u.msg("请选择操作员");
				if (!this.dataForm.produceTime) return this.$u.msg("请选择生产日期");
				if (!this.dataForm.expireTime) return this.$u.msg("请选择过期日期");
				this.realSumbit()
			},
			realSumbit() {
				if (this.dataForm.id) {
					this.$http.put('/material/foodAddRecord', this.dataForm).then(res => {
						if (res.code == 200) {
							uni.$emit('reFlashList')
							this.$u.msg('修改成功')
							this.$u.back(1, 1000)
						}
					})
				} else {
					this.$http.post('/material/foodAddRecord', this.dataForm).then(res => {
						if (res.code == 200) {
							uni.$emit('reFlashList')
							this.$u.msg('已新增')
							this.$u.back(1, 1000)
						}
					})
				}
			}

		}
	}
</script>

<style>

</style>